import React from 'react'
import axios from 'axios'
function Index() {

  const [inputValue, setInputValue] = React.useState('')
  const [outputValue, setOutputValue] = React.useState('')


  const AI = async () => {
    try {
      const response = await axios.post(
        `https://api.moonshot.cn/v1/chat/completions`,
        {
          model: 'moonshot-v1-8k',
          // inputValue为用户输入信息
          messages: [
            { role: 'user', content: inputValue },
          ],
          temperature: 1,
        },
        {
          headers: {
            Authorization: `Bearer ${'sk-cTskS1TlOfNmHPmNFrj0Cj58C2MgLQjdwluTz92zVGnaYD4r'}`,
          },
        }
      )
      setOutputValue(response.data.choices[0]?.message?.content)
      // 读出来
      window.speechSynthesis.speak(new SpeechSynthesisUtterance(response.data.choices[0]?.message?.content))
      console.log(
        'AI:',
        response.data.choices[0]?.message?.content
      )
    } catch (error) {
      console.error('Error:', error)
    }
  }



  

  return (
    <div className='AI'>
      <input  type="text" value={inputValue} onChange={(e) => setInputValue(e.target.value)} />
      <button onClick={AI}>发送</button>
      <div>{outputValue}</div>
    </div>
  )
}

export default Index
